import http from '@/http'
import Apis from './types'

const modules:any = import.meta.glob('./modules/*.ts', { eager: true })
const apiModules:any = {}
Object.keys(modules).forEach((key) => {
  if (key.includes('/_')) {
    return
  }
  Object.assign(apiModules, modules[key].default)
})
type Methods = 'post' | 'get' | 'delete' | 'put'
const injectRequest = (apis:any) => {
  const request:any = {}
  Object.keys(apis).forEach((item) => {
    const { method, url } = apis[item]
    request[item] = (dataOrParams = {}) => {
      const param = ['POST'].includes(method.toUpperCase()) ? dataOrParams : { params: dataOrParams }
      return http[method.toLowerCase() as Methods](url, param)
    }
  })
  return request as Apis
}

export default injectRequest(apiModules)
